package com.yyfax.ph.home.recommendloan.creditinfo;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.YJPH;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * P:有无征信上传接口
 * Created by ying_he on 2017/11/7.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class UploadCreditPaperPresenter extends Presenter<UploadCreditPaperModel, UploadCreditPaperPresenter.View >{
	private static final String TAG = UploadCreditPaperPresenter.class.getSimpleName();
	private int mBizType;

	public static class Generator implements PresenterFactory<UploadCreditPaperPresenter> {
		@Override
		public UploadCreditPaperPresenter create() {
			return new UploadCreditPaperPresenter();
		}
	}

	public interface View extends IView {
		void uploadCreditSuccess(String msg);
		void uploadCreditFailed(String msg);
	}

	private View mView;

	@Override
	public void attach(UploadCreditPaperPresenter.View view) {
		mView = view;
		mModel = new UploadCreditPaperModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void uploadCreditPaper(String rcmId, List<String> imageIds,int bizType){
		UploadCreditPaperModel.CreditPaper creditPaper = new UploadCreditPaperModel.CreditPaper();
		creditPaper.rcmdId = rcmId;
		creditPaper.imageIds.clear();
		creditPaper.imageIds.addAll(imageIds);
		mBizType = bizType;
		execute(creditPaper, FUNCTIONAL_CODE, true);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void cancelResult(UploadCreditPaperModel.UploadCreditPaperResult result) {
		if(mBizType == UploadCreditPaperFragment.BIZ_TYPE_HAS_CREDIT_RECOMMEND_UPLOAD){
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_B004, result.getStatus());

		}else if(mBizType == UploadCreditPaperFragment.BIZ_TYPE_NO_CREDIT_RECOMMEND_UPLOAD){
			StatService.onEventFirstKeyAttribute(YJPH.getInstance(), StatIDSet.EVENT_ID_B005, result.getStatus());

		}
		if (isResponseSuccess(result)) {
			mView.uploadCreditSuccess(result.getMsg());
		}else {
			mView.uploadCreditFailed(result.getMsg());
		}
	}

}
